﻿Public Class fKalkNaklDetailZobrazeni
    Public cbox As ComboBox = My.Forms.fMain.cboxZvolitKalkVyrShow

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub fKalkNaklDetailZobrazeni_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub

    Private Sub fKalkNaklDetailZobrazeni_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Text = "Detailní kalkulace výrobku: " & cbox.SelectedItem.ToString 'set form header text
        'loads needed tables
        Me.Seznam_vstupyTableAdapter1.Fill(Me.DtbDataSet1.seznam_vstupy)
        Me.Seznam_vyrobkyTableAdapter1.Fill(Me.DtbDataSet1.seznam_vyrobky)
        Me.Tab_kalkulaceTableAdapter1.Fill(Me.DtbDataSet1.tab_kalkulace)
        'creates needed columns
        Dim colJmVstup As New DataColumn() With {.ColumnName = "colJmVstup", .DataType = GetType(String)}
        Dim colCenaKusVstup As New DataColumn() With {.ColumnName = "colCenaKusVstup", .DataType = GetType(Decimal)}
        Dim colMnVstup As New DataColumn() With {.ColumnName = "colMnVstup", .DataType = GetType(Decimal)}
        Dim colCelNaklVstup As New DataColumn() With {.ColumnName = "colCelNaklVstup", .DataType = GetType(Decimal)}
        Dim colKumNakl As New DataColumn() With {.ColumnName = "colKumNakl", .DataType = GetType(Decimal)}
        'add them to DGV
        DGV_KalkNaklPrehl.Rows.Clear()
        DGV_KalkNaklPrehl.Columns.Clear()

        With DGV_KalkNaklPrehl.Columns
            .Add(colJmVstup.ColumnName, "Název vstupu")
            .Add(colCenaKusVstup.ColumnName, "Cena/kus")
            .Add(colMnVstup.ColumnName, "Množství vstupu")
            .Add(colCelNaklVstup.ColumnName, "Celkový náklad vstupu")
            .Add(colKumNakl.ColumnName, "Kumulované náklady")
        End With
        DGV_KalkNaklPrehl.Update()
        'getting items of selected item's calculation
        Dim kalkulace As DataTable = Me.DtbDataSet1.tab_kalkulace
        Dim vyrobky As DataTable = Me.DtbDataSet1.seznam_vyrobky
        Dim vstupy As DataTable = Me.DtbDataSet1.seznam_vstupy
        Dim item As String = cbox.SelectedItem.ToString
        Dim itemID As Long = Nothing
        Dim itemRows(-1) As DataRow
        Dim itemRowCntr As Long = 0
        '-----get product id
        Try
            For i As Long = 0 To vyrobky.Rows.Count() - 1
                If vyrobky.Rows.Item(i).Item(1) = item Then
                    itemID = vyrobky.Rows.Item(i).Item(0)
                    Exit For
                End If
            Next i
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba při zjišťování jména produktu")
            Me.Close()
            Exit Sub
        End Try
        '-----get number of rows and rows of the product in tab_kalkulace
        Try
            For i As Long = 0 To kalkulace.Rows.Count() - 1
                If kalkulace.Rows.Item(i).Item(1) = itemID Then
                    ReDim Preserve itemRows(itemRowCntr)
                    itemRows(itemRowCntr) = kalkulace.Rows.Item(i)
                    itemRowCntr += 1
                End If
            Next i
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba při zjišťování počtu vstupů")
            Me.Close()
            Exit Sub
        End Try
        '-----create new rows, fill them and save them to DGV table
        Try
            Dim arrForCellsPrep(itemRows.GetUpperBound(0), 4) As Object
            For i As Long = 0 To arrForCellsPrep.GetUpperBound(0)
                'get cells for 1st column with inputs's names
                For k As Long = 0 To vstupy.Rows.Count() - 1
                    If vstupy.Rows.Item(k).Item(0) = _
                        itemRows(i).Item(2) Then
                        arrForCellsPrep(i, 0) = vstupy.Rows.Item(k).Item(1)
                        Exit For
                    End If
                Next k
                'get cells for 2nd column with unit price
                arrForCellsPrep(i, 1) = Math.Round(itemRows(i).Item(3), 2)
                'get cells for 3rd column with quantity of input
                arrForCellsPrep(i, 2) = Math.Round(itemRows(i).Item(4), 4)
                'get total input's costs for 4th column
                arrForCellsPrep(i, 3) = Math.Round(arrForCellsPrep(i, 1) * arrForCellsPrep(i, 2), 2)
                'get cumulative sum of costs for 5th column
                If i = 0 Then
                    arrForCellsPrep(i, 4) = Math.Round(arrForCellsPrep(i, 3), 2)
                Else
                    arrForCellsPrep(i, 4) = Math.Round(arrForCellsPrep(i - 1, 4) + arrForCellsPrep(i, 3), 2)
                End If
            Next i
            '---end prep filling, now create rows of cells for DGV
            For i As Long = 0 To arrForCellsPrep.GetUpperBound(0)
                Dim arrFill(arrForCellsPrep.GetUpperBound(1)) As Object

                For k As Long = 0 To arrFill.GetUpperBound(0)
                    arrFill(k) = arrForCellsPrep(i, k)
                Next k

                Dim DGVrow As New DataGridViewRow() With {.ReadOnly = True}
                DGVrow.CreateCells(DGV_KalkNaklPrehl, arrFill)
                DGV_KalkNaklPrehl.Rows.Add(DGVrow)
            Next i
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Chyba při vytváření řádků přehledu")
            Me.Close()
            Exit Sub
        End Try
        DGV_KalkNaklPrehl.Update()
        DGV_KalkNaklPrehl.Enabled = True
        DGV_KalkNaklPrehl.Visible = True
        DGV_KalkNaklPrehl.Show()

    End Sub
End Class